#include "iooperationimpl.h"

CIoOperationImpl::CIoOperationImpl()
{
    m_pBufLen = 4 * 1024;
    m_pBuf = NULL;
    m_pBuf = ALLOC_NEW BYTE[m_pBufLen + 1]();
    m_uStartPos = 0;
    m_uExpectBytes = 0;
}
CIoOperationImpl::~CIoOperationImpl()
{
    m_pEvent.dispose();
    logi("CIoOperationImpl::~CIoOperationImpl");
}
HRESULT CIoOperationImpl::BindIBase(IBase* pBase)
{
    rc_error(pBase != INULL, E_FAIL);
    m_pBase.dispose();
    return pBase->QueryInterface(IID_IBase, (void**)&m_pBase);
}
HRESULT CIoOperationImpl::UnBindIBase()
{
    m_pBase.dispose();
    return S_OK;
}
HRESULT CIoOperationImpl::QueryIBaseIID(REFIID riid, void** ppv)
{
    rc_error(m_pBase.m_p != INULL, E_FAIL);
    return m_pBase->QueryInterface(riid, (void**)ppv);
}
HRESULT CIoOperationImpl::BindEvent(IBase* pEvents)
{
    HRESULT hr = S_OK;
    hr = pEvents->QueryInterface(IID_IOperationProc, (void**)&m_pEvent);
    rc_error(hr == S_OK, E_FAIL);
    return hr;
}
HRESULT CIoOperationImpl::UnBindEvent()
{
    m_pEvent.dispose();
    return S_OK;
}
HRESULT CIoOperationImpl::SetId(crtid id)
{
    m_cid = id;
    return S_OK;
}
crtid CIoOperationImpl::GetId()
{
    return m_cid;
}
HRESULT CIoOperationImpl::SetResult(crterr err, buf_len len)
{
    m_err = err;
    m_pTransferedBytes = len;
    return S_OK;
}
HRESULT CIoOperationImpl::GetResult(crterr* err, buf_len* len)
{
    *err = m_err;
    *len = m_pTransferedBytes;
    return S_OK;
}
HRESULT CIoOperationImpl::SetIoParam(buf_len pos, buf_len len)
{
    if (len - pos > m_pBufLen) {
        return E_FAIL;
    }
    m_uStartPos = pos;
    m_uExpectBytes = len;
    return S_OK;
}
HRESULT CIoOperationImpl::GetIoParam(buf_len* pos, buf_len* len)
{
    *pos = m_uStartPos;
    *len = m_uExpectBytes;
    return S_OK;
}
HRESULT CIoOperationImpl::SetExpect(buf_len len)
{
    m_uExpectBytes = len;
    return S_OK;
}
HRESULT CIoOperationImpl::GetExpect(buf_len* len)
{
    *len = m_uExpectBytes;
    return S_OK;
}
HRESULT CIoOperationImpl::SetPos(buf_len pos)
{
    m_uStartPos = pos;
    return S_OK;
}
HRESULT CIoOperationImpl::GetPos(buf_len* pos)
{
    *pos = m_uStartPos;
    return S_OK;
}
HRESULT CIoOperationImpl::SetTransfered(buf_len len)
{
    m_pTransferedBytes = len;
    return S_OK;
}
HRESULT CIoOperationImpl::GetTransfered(buf_len* len)
{
    *len = m_pTransferedBytes;
    return S_OK;
}
HRESULT CIoOperationImpl::Attach(buf_ptr ptr, buf_len pos, buf_len size)
{
    rc_error(ptr != NULL, E_FAIL);
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    s_memcpy(m_pBuf.m_p + pos, ptr, size);
    return SetIoParam(pos, size);
}
HRESULT CIoOperationImpl::Alloc(buf_len size)
{
    m_pBuf.dispose();
    m_pBufLen = size;
    m_pBuf = ALLOC_NEW unsigned char[m_pBufLen + 1]();
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    return S_OK;
}
HRESULT CIoOperationImpl::GetLen(buf_len* len)
{
    *len = m_pBufLen;
    return S_OK;
}
HRESULT CIoOperationImpl::GetPtr(buf_ptr* ptr)
{
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    *ptr = m_pBuf.m_p;
    return S_OK;
}
HRESULT CIoOperationImpl::GetOperationPtr(buf_ptr* ptr, buf_len* len)
{
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    *ptr = m_pBuf.m_p;
    *len = m_pBufLen;
    return S_OK;
}
HRESULT CIoOperationImpl::ReleasePtr()
{
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    m_pBuf.dispose();
    m_pBufLen = 0;
    return S_OK;
}
HRESULT CIoOperationImpl::ClearPtr()
{
    rc_error(m_pBuf.m_p != NULL, E_FAIL);
    s_memset(m_pBuf, 0, m_pBufLen);
    return S_OK;
}
HRESULT CIoOperationImpl::Post(crtid cid, crterr err)
{
    rc_error(m_pEvent.m_p != INULL, E_FAIL);
    return m_pEvent->OnComplete(this, cid, err);
}
